﻿using IdentityData.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Service
{

    public class AccountService
    {
        private readonly UserManager<ApplicationUser> _userManager;
        public AccountService(UserManager<ApplicationUser> userManager)
        {
            _userManager = userManager;
        }

        /// <summary>
        /// 获取全部用户
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<List<ApplicationUser>> FindEntitiesAsync(Expression<Func<ApplicationUser, bool>> where)
        {
            var users = await _userManager.Users.Where(where).ToListAsync();
            return users;
        }

        /// <summary>
        /// 获取单个用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApplicationUser> FindByIdAsync(string id)
        {
            return await _userManager.FindByIdAsync(id);
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<IdentityResult> UpdateAsync(ApplicationUser user)
        {
            var result = await _userManager.UpdateAsync(user);
            return result;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<IdentityResult> DeleteAsync(ApplicationUser user)
        {
            var result = await _userManager.DeleteAsync(user);
            return result;
        }

        /// <summary>
        /// 账户锁定、解锁控制
        /// </summary>
        /// <param name="user"></param>
        /// <param name="enabled"></param>
        /// <returns></returns>
        public async Task<IdentityResult> SetLockoutEnabledAsync(ApplicationUser user,bool enabled)
        {
            var result= await _userManager.SetLockoutEnabledAsync(user, enabled);
            return result;
        }

        /// <summary>
        /// 将指定用户添加到命名角色
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="roles">角色名称集合</param>
        /// <returns></returns>
        public async Task<IdentityResult> AddToRolesAsync(ApplicationUser user,IEnumerable<string> roles)
        {
            var result=await _userManager.AddToRolesAsync(user, roles);
            return result;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<IdentityResult> CreateAsync(ApplicationUser user, string password)
        {
            var result = await _userManager.CreateAsync(user, password);
            return result;
        }
    }
}
